# 09/10/2022 ;
# plots descriptive overviews (Figures 1 and 2) ;
library(broom)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(ordinal)
library(stringr)
library(tidyverse)

theme_set(theme_tufte())

#-------------------------------------------------------------------------------
# LOAD RECODED ESS-8 DATASET ;
#-------------------------------------------------------------------------------
rm(list=ls())
load("DATA_ESS.RData")

# CREATE FACTORS AND FACTOR LABELS ;
ess_long$sanction  <- factor(ess_long$sanction,  
                             labels=c("O1: keine Kürzungen",
                                      "O2: geringe Kürzungen",
                                      "O3: Kürzung um die Hälfte",
                                      "O4: Streichung"))

ess_long$situation <- factor(ess_long$situation,
                             labels=c("S1: geringere Bezahlung", 
                                      "S2: geringere Qualifikation", 
                                      "S3: gemeinnützige Arbeit"))

ess_long$vignette  <- factor(ess_long$vignette,
                             labels=c("T1: 20-25 Jahre",
                                      "T2: unspezifiziert",
                                      "T3: 50-59 Jahre",
                                      "T4: alleinerziehend"))

#-------------------------------------------------------------------------------
# FIGURE 1 ;
#-------------------------------------------------------------------------------

FIG_TREAT_PAY <- 
  filter(ess_long, situation=="S1: geringere Bezahlung") %>%
  count(vignette, sanction) %>%
  group_by(vignette) %>%
  mutate(n = n/sum(n) * 100) %>%
  ggplot() + aes(vignette, n, fill = sanction, label = paste0(round(n, 2), "%")) + 
  geom_col() +
  geom_text(position=position_stack(0.5)) +
  xlab("") + 
  ylab("") + 
  theme(legend.position="bottom") + 
  scale_y_continuous() +
  ggtitle("S1: geringere Bezahlung") +
  scale_fill_brewer(palette = "Greys", name="Sanktion (O):") 

FIG_TREAT_EDU <- 
  filter(ess_long, situation=="S2: geringere Qualifikation") %>%
  count(vignette, sanction) %>%
  group_by(vignette) %>%
  mutate(n = n/sum(n) * 100) %>%
  ggplot() + aes(vignette, n, fill = sanction, label = paste0(round(n, 2), "%")) + 
  geom_col() +
  geom_text(position=position_stack(0.5)) +
  xlab("") + 
  ylab("") + 
  theme(legend.position="bottom") + 
  scale_y_continuous() +
  ggtitle("S2: geringere Qualifikation") +
  scale_fill_brewer(palette = "Greys", name="Sanktion (O):") 

FIG_TREAT_UNP <- 
  filter(ess_long, situation=="S3: gemeinnützige Arbeit") %>%
  count(vignette, sanction) %>%
  group_by(vignette) %>%
  mutate(n = n/sum(n) * 100) %>%
  ggplot() + aes(vignette, n, fill = sanction, label = paste0(round(n, 2), "%")) + 
  geom_col() +
  geom_text(position=position_stack(0.5)) +
  xlab("") + 
  ylab("") + 
  theme(legend.position="bottom") + 
  scale_y_continuous() +
  ggtitle("S3: gemeinnützige Arbeit") +
  scale_fill_brewer(palette = "Greys", name="Sanktion (O):") 

FIG_TREAT <- plot_grid(FIG_TREAT_PAY + theme(legend.position="none"), 
                       FIG_TREAT_EDU + theme(legend.position="none"), 
                       FIG_TREAT_UNP + theme(legend.position="none"), 
                       align = 'vh',
                       nrow = 1)

legend <- get_legend(
  FIG_TREAT_PAY + 
    guides(color = guide_legend(nrow = 1)) +
    theme(legend.position = "bottom")
)

FIG_TREAT <- plot_grid(FIG_TREAT, legend, ncol = 1, rel_heights = c(1, .1))

ggsave("FIG_1.png", FIG_TREAT, width=18, height=9)

#-------------------------------------------------------------------------------
# FIGURE 2 ;
#-------------------------------------------------------------------------------

FIG_COUNTRY <- 
  ess_long %>%
  count(cntry, sanction) %>%
  group_by(cntry) %>%
  mutate(n = n/sum(n) * 100) %>%
  ggplot() + aes(cntry, n, fill = sanction, label = paste0(round(n, 2), "%")) + 
  geom_col() +
  geom_text(position=position_stack(0.5)) +
  xlab("") + 
  ylab("") + 
  theme(legend.position="bottom") + 
  scale_y_continuous() +
  ggtitle("Vorgeschlagene Sanktionen über drei stilisierte Kontexte") +
  scale_fill_brewer(palette = "Greys", name="Sanktion (O):") 

ggsave("FIG_2.png", FIG_COUNTRY, width=18, height=9)
